//
//  KZMineTableViewContriller.m
//  xiaorizi
//
//  Created by kun on 15/10/29.
//  Copyright © 2015年 origin. All rights reserved.
//

#import "KZMineTableViewContriller.h"
#import "KZMineHeadView.h"
#import "UIImageView+WebCache.h"

#import "KZContentFile.h"
#import "KZMineCell.h"
#import "EveryDayModel.h"
#import "KZEventsViewController.h"

@interface KZMineTableViewContriller ()

@property (weak, nonatomic) IBOutlet KZMineHeadView *headerView;
@property (strong, nonatomic) KZContentFile *manger;
@end

@implementation KZMineTableViewContriller

- (void)viewDidLoad {
    [super viewDidLoad];
    
    //添加headView
    //1头部
    //获取数据
    _manger = [KZContentFile shareInstance];
    self.title = @"我的";
    UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc]initWithTarget:self action:@selector(clickedHeaderView:)];
    [self.headerView addGestureRecognizer:tap];
   }

-(void)viewWillAppear:(BOOL)animated
{
    [super viewWillAppear:animated];
    NSUserDefaults *user = [NSUserDefaults standardUserDefaults];
    //NSLog(@"--->%@",[user objectForKey:@"LoginImg"]);
    if([user objectForKey:@"isLogin"])
    {
        self.headerView.nameLabel.text = [user objectForKey:@"LoginName"];
        
        [self.headerView.UserLoginImg sd_setImageWithURL:[NSURL URLWithString:[user objectForKey:@"LoginImg"]]];
    }
    
    //重新加载tableView
    [self.tableView reloadData];
}

-(void)viewWillDisappear:(BOOL)animated
{
    [super viewWillDisappear:animated];
    self.headerView.nameLabel.text = @".坤";
    [self.headerView.UserLoginImg setImage:[UIImage imageNamed:@"logo_s"]];
}



#warning 登陆实现可以用AOP切面思想的 实现？

- (void)clickedHeaderView:(UITapGestureRecognizer *)tap
{
    //cell 自带事件触发机制 方法
    //判断死否已经登陆
    self.hidesBottomBarWhenPushed = YES;
    if([[NSUserDefaults standardUserDefaults] objectForKey:@"isLogin"])
    {
        
    }
    else
    {
        [self performSegueWithIdentifier:@"showLogin" sender:nil];
    
    }
    self.hidesBottomBarWhenPushed = NO;
}


//数据记载加载收藏的内容



- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

#pragma mark - Table view data source

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
#warning Incomplete implementation, return the number of rows
    return [_manger contentDataCount];
}


- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    KZMineCell *cell = [tableView dequeueReusableCellWithIdentifier:@"collectCell" forIndexPath:indexPath];
    
    cell.model = [_manger contentDataAtIndex:indexPath.row];
    // Configure the cell...
    return cell;
}


//点击cell跳转

-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    //获取model
    EveryDayModel *model = [_manger contentDataAtIndex:indexPath.row];
    
    //跳转
    UIStoryboard *discoverSB = [UIStoryboard storyboardWithName:@"KZEveryDvdTableViewController" bundle:nil];
    KZEventsViewController *discoverV = [discoverSB instantiateViewControllerWithIdentifier:@"KZEventsViewController"];
    discoverV.model = model;
    [self.navigationController pushViewController:discoverV animated:YES];
    
}




/*
// Override to support conditional editing of the table view.
- (BOOL)tableView:(UITableView *)tableView canEditRowAtIndexPath:(NSIndexPath *)indexPath {
    // Return NO if you do not want the specified item to be editable.
    return YES;
}
*/

/*
// Override to support editing the table view.
- (void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath {
    if (editingStyle == UITableViewCellEditingStyleDelete) {
        // Delete the row from the data source
        [tableView deleteRowsAtIndexPaths:@[indexPath] withRowAnimation:UITableViewRowAnimationFade];
    } else if (editingStyle == UITableViewCellEditingStyleInsert) {
        // Create a new instance of the appropriate class, insert it into the array, and add a new row to the table view
    }   
}
*/

/*
// Override to support rearranging the table view.
- (void)tableView:(UITableView *)tableView moveRowAtIndexPath:(NSIndexPath *)fromIndexPath toIndexPath:(NSIndexPath *)toIndexPath {
}
*/

/*
// Override to support conditional rearranging of the table view.
- (BOOL)tableView:(UITableView *)tableView canMoveRowAtIndexPath:(NSIndexPath *)indexPath {
    // Return NO if you do not want the item to be re-orderable.
    return YES;
}
*/

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
